package com.elinkway.infinitemovies.adapter;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.elinkway.infinitemovies.bean.Favorite;
import com.elinkway.infinitemovies.dao.FavoriteDao;
import com.elinkway.infinitemovies.ui.activity.FavoriteActivity;
import com.elinkway.infinitemovies.utils.MoviesConstant;
import com.le123.ysdq.R;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/*
 * author qinguoli
 * 
 * */
public class FavoriteListAdapter extends MyBaseAdapter {

	public List<Favorite> list;
	private Boolean isEditing = false;
	public FavoriteDao favoriteDao;
	
	public boolean deleteState = false;
	
	//红点状态
//	public ArrayList<Boolean> redDotList;
	//toggleButton选中状态
	public ArrayList<Boolean> mCheckedList;
	//当前集数
//	public ArrayList<Integer> nowEpisodesList;
	public int deletedNum;
	
	public boolean allSelectState = false;
	
	private FavoriteActivity favoriteActivity;

	private class ItemHolder {
		ToggleButton favorite_delete;//删除按钮
		ImageView leftImageIv;
		TextView tv_title;
		TextView tv_starring;
		TextView tv_update;
		ImageView img_update_hint;//更新提示红点
	}

	public FavoriteListAdapter(FavoriteActivity context,
			List<Favorite> list) {
		super(context);
		
		favoriteActivity = context;
		mCheckedList = new ArrayList<Boolean>();
		
		for(int i = 0; i < list.size(); i ++){
			
			mCheckedList.add(false);
			//mCheckedList.add(true);
		}
		deletedNum = 0;
		this.list = list;
		favoriteDao = new FavoriteDao(mContext);
	}

	public void updateData(List<Favorite> favoriteList) {
		if(list != null) {
			list.clear();
			list.addAll(favoriteList);
			notifyDataSetChanged();
		}
	}
	public void toggle() {
		isEditing = !isEditing;
		notifyDataSetChanged();
	}
	
	public boolean getEditState(){
		return isEditing;
	}
	
	public void setToggleButtonChecked(int position){
		
		mCheckedList.set(position, true); 
	}
	
	public void setToggleButtonUnchecked(int position){
		mCheckedList.set(position, false); 
	}
	
	public void deleteNumDecrease(){
		deletedNum ++;
	}
	
	public void deleteNumIncrease(){
		deletedNum --;
	}
	
	
	// 选中项状态变化
	public void setSelectItemChange(int position, View view) {

		if (mCheckedList.get(position)) {

			deletedNum--;
			mCheckedList.set(position, false);
		} else {

			deletedNum++;
			mCheckedList.set(position, true);
		}

		favoriteActivity.setTitle();

	}
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
//		Favorite favorite = new Favorite();
		Favorite favorite = list.get(position);
		ItemHolder item = new ItemHolder();
		if (convertView == null) {
			
			convertView = inflater.inflate(R.layout.favorite_list_item_new, null);
			item = new ItemHolder();
			item.favorite_delete= (ToggleButton)convertView.findViewById(R.id.favoritedelete);
			item.leftImageIv = (ImageView) convertView.findViewById(R.id.left_image_iv);
			item.tv_title = (TextView) convertView
					.findViewById(R.id.favorite_title);
			item.tv_starring = (TextView) convertView
					.findViewById(R.id.favorite_starring);
			item.tv_update = (TextView)convertView.findViewById(R.id.favorite_update);
			item.img_update_hint = (ImageView) convertView
					.findViewById(R.id.favorite_update_hint);
			
			convertView.setTag(item);
			
		} else {
			item = (ItemHolder) convertView.getTag();
		}

		if(!TextUtils.isEmpty(favorite.getPoster())){
			ImageLoader.getInstance().displayImage(favorite.getPoster(), item.leftImageIv);
		}else{
			item.leftImageIv.setImageResource(R.drawable.ic_launcher);
		}
		item.tv_title.setText( favorite.getName());
		
		item.tv_starring.setText(getVtDescription(favorite.getVt()));
		
		item.tv_update.setText(getNowEpisodesDescription(favorite));
		
		updateToggleButtonState(item, position);

		if (isEditing) {//编辑状态
			
			item.favorite_delete.setVisibility(View.VISIBLE);
			item.img_update_hint.setVisibility(View.GONE);
		} else {
			if(favorite.isUpdated()){
				item.img_update_hint.setVisibility(View.VISIBLE);
			}else{
				item.img_update_hint.setVisibility(View.GONE);
			}
			item.favorite_delete.setVisibility(View.GONE);
		}
		return convertView;
	}
	
	
	private String getVtDescription(String vt){
		
		String vtDescription = null;
		if(TextUtils.equals(vt, MoviesConstant.VT_MOVIE)){
			
			vtDescription = MoviesConstant.NAME_VT_MOVIE;
		}else if(TextUtils.equals(vt, MoviesConstant.VT_TV)){
			
			vtDescription = MoviesConstant.NAME_VT_TV + "  |  ";
		}else if(TextUtils.equals(vt, MoviesConstant.VT_CARTOON)){
			
			vtDescription = MoviesConstant.NAME_VT_CARTOON + "  |  ";
		}else if(TextUtils.equals(vt, MoviesConstant.VT_ZONGYI)){
			vtDescription = MoviesConstant.NAME_VT_ZONGYI + "  |  ";
			
		}
		else{
			return "";
		}
		
		return vtDescription ;
		
	}
	
	private String getNowEpisodesDescription(Favorite favorite){
		
		String nowEpisodesDescription;
		
		if(TextUtils.equals(favorite.getVt(), MoviesConstant.VT_MOVIE)){
			
			nowEpisodesDescription = "";
		}else if(TextUtils.equals(favorite.getVt(), MoviesConstant.VT_TV)|| TextUtils.equals(favorite.getVt(), MoviesConstant.VT_CARTOON)){
			
			if(TextUtils.equals("1", favorite.getIsend())){
				nowEpisodesDescription = mContext.getString(R.string.favorite_end_episodes,favorite.getLatestEpisodes());
			}else{
				nowEpisodesDescription = mContext.getString(R.string.favorite_updating_episodes,favorite.getLatestEpisodes());
				
			}
			
		}else if(TextUtils.equals(favorite.getVt(), MoviesConstant.VT_ZONGYI)){
			
			nowEpisodesDescription =mContext.getString(R.string.favorite_updating_episodes_zongyi,favorite.getLatestEpisodes());
		}
		else{
			nowEpisodesDescription = mContext.getString(R.string.favorite_updating_episodes,favorite.getLatestEpisodes());
		}
		
		
		return nowEpisodesDescription;
	}
	
	
	//更新ToggleButton选中状态
	private void updateToggleButtonState(ItemHolder holder, int position) {
		if(!deleteState){
			holder.favorite_delete.setVisibility(View.VISIBLE);
			if(position >= mCheckedList.size()) {
				for(int i = mCheckedList.size()-1; i < position; i++) {
					mCheckedList.add(false);
				}
			}
			if(position < mCheckedList.size())
				holder.favorite_delete.setChecked(mCheckedList.get(position));
			
		}else{
			holder.favorite_delete.setVisibility(View.GONE);
		
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}
	
	
	
	
	
	
}
